#include "SdkHardware.h"

Com_void Ext_SdkGpio_Init(Com_void)
{
}

Com_s32 Ext_SdkGpio_Write_Str(Com_ps8 pin, Com_s32 value)
{
    const Com_s8 values_str[] = "01";
    Com_s8 path[64];
    Com_s32 fd;

    memset(path,0,sizeof(path));

    snprintf(path, sizeof(path), "%s", pin);

    if(access(path, F_OK) == 0)
    {
        fd = open(path, O_WRONLY);
        if (fd < 0) {
            printf("Failed to open %s for writing!\n",pin);
            return -1;
        }

        if (write(fd, &values_str[value == 0 ? 0 : 1], 1) < 0)
        {
            printf("Failed to write %s value!\n",pin);
            close(fd);
            return -1;
        }
        close(fd);
    }
    return 0;
}

Com_s32 Ext_SdkGpio_Read_Str(Com_ps8 pin)
{
    Com_s8 path[64];
    Com_s8 value_str[3];
    Com_s32 fd;

    memset(path,0,sizeof(path));
    memset(value_str,0,sizeof(value_str));

    snprintf(path, sizeof(path), "%s", pin);

    if(access(path, F_OK) == 0)
    {
        fd = open(path, O_RDONLY);
        if (fd < 0)
        {
            printf("Failed to open %s for reading!\n",pin);
            return -1;
        }

        if (read(fd, value_str, 3) < 0)
        {
            printf("Failed to read %s value!\n",pin);
            close(fd);
            return -1;
        }
        close(fd);
        return (atoi(value_str));
    }

    return 0;
}

void Ext_SdkGpio_Write_Led(int value)
{
    const char values_str[] = "01";
    char path[64];
    int fd;

    snprintf(path, sizeof(path), "/sys/class/leds/system_state_led/brightness");
    fd = open(path, O_WRONLY);
    if (fd < 0) {
        printf("Failed to led brightness for writing!\n");
        return;
    }

    if (write(fd, &values_str[value == 0 ? 0 : 1], 1) < 0) {
        printf("Failed to write value!\n");
        return;
    }

    close(fd);
}

void Ext_SdkGpio_Write_Spk(int value)
{
    const char values_str[] = "01";
    char path[64];
    int fd;

    snprintf(path, sizeof(path), "/sys/user-gpio/spk_en");
    fd = open(path, O_WRONLY);
    if (fd < 0) {
        printf("Failed to spk_en for writing!\n");
        return;
    }

    if (write(fd, &values_str[value == 0 ? 0 : 1], 1) < 0) {
        printf("Failed to write value!\n");
        return;
    }

    close(fd);
}

void Ext_SdkGpio_Write_IRLed(int value)
{
    const char values_str[] = "01";
    char path[64];
    int fd;

    snprintf(path, sizeof(path), "/sys/user-gpio/ir-led");
    fd = open(path, O_WRONLY);
    if (fd < 0) {
        printf("Failed to ir-led for writing!\n");
        return;
    }

    if (write(fd, &values_str[value == 0 ? 0 : 1], 1) < 0) {
        printf("Failed to write value!\n");
        return;
    }

    close(fd);
}

void Ext_SdkGpio_Write_IRCut(int mode)
{
    const char values_str[] = "01";
    char path[64];
    int fd;

    Com_s32 val = Ext_InSdkBasicCfg_GetVal(BASIC_CFG_IRCUT);
    if(val == 1)
    {
        val = Ext_InSdkBasicCfg_GetVal(BASIC_CFG_IRCUT_REVERSE);
        if(val == 1)
        {
            if (mode == IRCUT_DAY_MODE)
            {
                snprintf(path, sizeof(path), "/sys/user-gpio/ircut_n");
                fd = open(path, O_WRONLY);
                if (fd < 0) {
                    printf("Failed to ircut_n for writing!\n");
                    return;
                }
            }
            else
            {
                snprintf(path, sizeof(path), "/sys/user-gpio/ircut_p");
                fd = open(path, O_WRONLY);
                if (fd < 0) {
                    printf("Failed to ircut_p for writing!\n");
                    return;
                }
            }
        }
        else
        {
            if (mode == IRCUT_DAY_MODE)
            {
                snprintf(path, sizeof(path), "/sys/user-gpio/ircut_p");
                fd = open(path, O_WRONLY);
                if (fd < 0) {
                    printf("Failed to ircut_p for writing!\n");
                    return;
                }
            }
            else
            {
                snprintf(path, sizeof(path), "/sys/user-gpio/ircut_n");
                fd = open(path, O_WRONLY);
                if (fd < 0) {
                    printf("Failed to ircut_n for writing!\n");
                    return;
                }
            }
        }

        if (write(fd, &values_str[1], 1) < 0) {
            printf("Failed to write 1!\n");
            return;
        }

        usleep(150 * 1000);

        if (write(fd, &values_str[0], 1) < 0) {
            printf("Failed to write 0!\n");
            return;
        }

        close(fd);
    }
}

/* 获取ADC值 */
int Ext_SdkGpio_Read_Adc0(void)
{
    char path[64] = {0};
    char value_str[6] = {0};
    int fd;

    snprintf(path, sizeof(path),"/sys/kernel/ain/ain0");
    fd = open(path, O_RDONLY);
    if (fd < 0) {
        printf("Failed to open feed_ir_det for reading!\n");
        return -1;
    }

    if (read(fd, value_str,6) < 0) {
        printf("Failed to read value!\n");
        return -1;
    }

    close(fd);
    return (atoi(value_str));
}
